package com.example.test001.web;

import com.alibaba.fastjson.JSON;
import com.example.test001.build.shiro.commons.CacheReloadUtil;
import com.example.test001.build.shiro.commons.MySessionDao;
import com.example.test001.build.shiro.commons.ShiroUtils;
import com.example.test001.build.shiro.realm.MyRealm;
import com.example.test001.dao.SysMenuMapper;
import com.example.test001.model.User;
import com.example.test001.service.UserService;
import com.example.test001.service.impl.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.SubjectContext;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Controller
public class WebController extends BaseController {


    @Autowired
    IUserService iUserService;

    @Autowired
    UserService userService;

    @Autowired
    MySessionDao sessionDAO;


    @Autowired
    Realm realm;


    @ResponseBody
    @RequiresAuthentication
    @GetMapping("abc")
    public Object get(Date date) {
        log.info(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
        return "abc";
    }




    @ResponseBody
    @GetMapping("/p")
    public Object get(){
        return  SecurityUtils.getSubject().getPrincipals();
    }


    @GetMapping("/login")
    public Object loginPage() {

        return "login";
    }

    @PostMapping("/login")
    public void login(User user) {

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken();

        token.setUsername(user.getUsername());

        token.setPassword(user.getPassword().toCharArray());

        subject.login(token);

    }



    @GetMapping("/index")
    public String indexPage(ModelMap map) {
        map.put("menus",iUserService.getMenus());
        return "index";
    }


    @RequiresRoles("abc")
    @ResponseBody
    @GetMapping("/index1")
    public String indexPage1() {
        return "index";
    }




}
